package cn.edu.scau.cmi.ema.service;

import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;


import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;

import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.persistence.Query;
import java.util.Iterator;
@Service("EventquantitytypeService")
@Transactional

public class EventquantitytypeServiceImpl extends CmiEmaService implements EventquantitytypeService {
	public EventquantitytypeServiceImpl() {
	}

	@Transactional
	public Eventquantitytype saveEventquantitytype(Eventquantitytype eventquantitytype) {

		if (eventquantitytype.getId() == null) {
			Event event = eventDAO.findEventById(eventquantitytype.getEvent().getId());
			Quantitytype quantitytype = quantitytypeDAO
					.findQuantitytypeById(eventquantitytype.getQuantitytype().getId());

			Eventquantitytype existingEventquantitytype = eventquantitytypeDAO
					.findEventquantitytypeByPhysicalPrimaryKey(event.getId(), quantitytype.getId(),
							eventquantitytype.getQuantity());

			eventquantitytype.setEvent(event);
			eventquantitytype.setQuantitytype(quantitytype);

			if (isEquals(eventquantitytype, existingEventquantitytype)) {
				return existingEventquantitytype;
			}

			eventquantitytype = eventquantitytypeDAO.store(eventquantitytype);
			eventquantitytypeDAO.flush();

		} else {
			Eventquantitytype existingEventquantitytype = eventquantitytypeDAO
					.findEventquantitytypeByPrimaryKey(eventquantitytype.getId());

			eventquantitytype = eventquantitytypeDAO.store(eventquantitytype);
			eventquantitytypeDAO.flush();
		}

		return eventquantitytype;
	}

	public void deleteEventquantitytype(Eventquantitytype eventquantitytype) {
		eventquantitytypeDAO.remove(eventquantitytype);
		eventquantitytypeDAO.flush();
	}

	@Transactional
	public void deleteEventquantitytypes(Set<Integer> deleteEventquantitytypesId) {
		Query query = eventquantitytypeDAO.createNamedQuery("deleteEventquantitytypesById", -1, -1);
		query.setParameter(1, deleteEventquantitytypesId);
		query.executeUpdate();
	}

	@Transactional
	public Set<Eventquantitytype> loadEventquantitytypes() {
		return eventquantitytypeDAO.findAllEventquantitytypes();

	}

	@Transactional
	public Set<Eventquantitytype> loadReferenceEventquantitytypes(Set<Eventquantitytype> eventquantitytypes) {
		Iterator<Eventquantitytype> eventquantitytypesIterator = eventquantitytypes.iterator();
		Set<Eventquantitytype> referencedEventquantitytypes = new HashSet<>();
		while (eventquantitytypesIterator.hasNext()) {
			Eventquantitytype currentEventquantitytype = eventquantitytypesIterator.next();

		}
		return referencedEventquantitytypes;
	}

	public Set<Eventquantitytype> loadEventquantitytypesByIdSet(Set<Integer> eventquantitytypeIdSet) {

		Iterator<Integer> eventquantitytypeIdIterator = eventquantitytypeIdSet.iterator();
		Set<Eventquantitytype> eventquantitytypeSet = new HashSet<>();
		while (eventquantitytypeIdIterator.hasNext()) {
			Integer id = eventquantitytypeIdIterator.next();
			Eventquantitytype currentEventquantitytype = eventquantitytypeDAO.findEventquantitytypeById(id);
			eventquantitytypeSet.add(currentEventquantitytype);
		}
		return eventquantitytypeSet;
	}

	@Transactional
	public List<Eventquantitytype> findAllEventquantitytypes(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Eventquantitytype>(
				eventquantitytypeDAO.findAllEventquantitytypes(startResult, maxRows));
	}

	@Transactional
	public Eventquantitytype findEventquantitytypeByPrimaryKey(Integer id) {
		return eventquantitytypeDAO.findEventquantitytypeByPrimaryKey(id);
	}

	@Transactional
	public Integer countEventquantitytypes() {
		return ((Long) eventquantitytypeDAO.createQuerySingleResult("select count(o) from Eventquantitytype o")
				.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countEventquantitytypes(CmiPagination page) {
		String sql = CmiSqlUtil.createCountHql(page, Eventquantitytype.class);
		return ((Long) eventquantitytypeDAO.createQuerySingleResult(sql).getSingleResult()).intValue();
	}

	/**
	 * search for name with ajax
	 * 
	 */
	@Transactional
	public Integer countEventquantitytypesByName(String queryName) {
		Query query = eventquantitytypeDAO.createNamedQuery("countEventquantitytypesByName", -1, -1);
		query.setParameter(1, queryName);
		return new Long((long) query.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countEventquantitytypesByDetachedCriteria(CmiPagination page) {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Eventquantitytype.class);
		Session session = entityManager.unwrap(Session.class);
		detachedCriteria.setProjection(Projections.rowCount());
		return new Long((long) detachedCriteria.getExecutableCriteria(session).uniqueResult()).intValue();
	}

	public Map<String, ?> loadEventquantitytypeObjectReferencedSet(Eventquantitytype eventquantitytype) {
		HashMap<String, Object> objectReferencedSetMap = new HashMap<>();
		if (eventquantitytype.getEvent() != null) {

			Set<Integer> eventIds = new HashSet<Integer>();
			eventIds.add(eventquantitytype.getEvent().getId());
			objectReferencedSetMap.put("eventIds", eventIds);
		}
		if (eventquantitytype.getQuantitytype() != null) {

			Set<Integer> quantitytypeIds = new HashSet<Integer>();
			quantitytypeIds.add(eventquantitytype.getQuantitytype().getId());
			objectReferencedSetMap.put("quantitytypeIds", quantitytypeIds);
		}

		return objectReferencedSetMap;
	}

	public Map<String, ?> loadEventquantitytypeClassReferencedSet(Eventquantitytype eventquantitytype) {
		HashMap<String, Object> classReferencedSetMap = new HashMap<>();
		classReferencedSetMap.put("events", eventDAO.findAllEvents());
		classReferencedSetMap.put("quantitytypes", quantitytypeDAO.findAllQuantitytypes());

		return classReferencedSetMap;
	}

	public boolean isEquals(Eventquantitytype eventquantitytype, Object obj) {
		if (obj == eventquantitytype)
			return true;
		if (!(obj instanceof Eventquantitytype))
			return false;
		Eventquantitytype comparedEventquantitytype = (Eventquantitytype) obj;
		if (!CheckPropertyEqual(eventquantitytype.getEvent().getId(), comparedEventquantitytype.getEvent().getId())) {
			return false;
		}
		if (!CheckPropertyEqual(eventquantitytype.getQuantitytype().getId(),
				comparedEventquantitytype.getQuantitytype().getId())) {
			return false;
		}
		if (!CheckPropertyEqual(eventquantitytype.getQuantity(), comparedEventquantitytype.getQuantity())) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}

